package com.kd.juhaowan.ui;

import android.os.Bundle;
import android.text.Html;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.MessageInfoApi;
import com.kd.juhaowan.http.api.QuestionInfoApi;
import com.kd.juhaowan.http.api.ShequInfoApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.MineMessageModel;

import butterknife.BindView;
import butterknife.OnClick;

public class DocumentActivity extends BaseActivity {





    @BindView(R.id.activity_title)
    TextView titleView;
    @BindView(R.id.notice_info_context)
    WebView webView;

    @Override
    protected void reInitDate() {

    }

    //    String name ="";
//    String content = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); // 开启支持JavaScript
        webSettings.setSupportZoom(false); // 不支持缩放功能
        webSettings.setBuiltInZoomControls(false); // 不显示默认的缩放控件
        webSettings.setDisplayZoomControls(false); // 不显示自定义的缩放控件
        webSettings.setDomStorageEnabled(true); // 开启DOM存储API
        webSettings.setDatabaseEnabled(true); // 开启数据库API
        webSettings.setAppCacheEnabled(true); // 开启应用程序缓存API
        webSettings.setAllowFileAccessFromFileURLs(true); // 允许从文件URL加载本地文件
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webView.setBackgroundColor(0x00FFFFFF);
        if(getIntent().hasExtra("question_id")){
            String id = getIntent().getStringExtra("question_id");
            EasyHttp.post(this).api(new QuestionInfoApi(id)).request(new HttpCallbackProxy<HttpData<MineMessageModel>>(this){
                @Override
                public void onHttpSuccess(HttpData<MineMessageModel> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
//                    name =result.getData().getName();
//                    content= result.getData().getContent();
                    setView(result.getData());
                }
            });
        }else if (getIntent().hasExtra("message_id")){
            String id = getIntent().getStringExtra("message_id");
            EasyHttp.post(this).api(new MessageInfoApi(id)).request(new HttpCallbackProxy<HttpData<MineMessageModel>>(this){
                @Override
                public void onHttpSuccess(HttpData<MineMessageModel> result, boolean cache) {
                    super.onHttpSuccess(result, cache);

                    setView(result.getData());
                }
            });
        }else if (getIntent().hasExtra("shequ_id")){
            String id = getIntent().getStringExtra("shequ_id");
            EasyHttp.post(this).api(new ShequInfoApi(id)).request(new HttpCallbackProxy<HttpData<MineMessageModel>>(this){
                @Override
                public void onHttpSuccess(HttpData<MineMessageModel> result, boolean cache) {
                    super.onHttpSuccess(result, cache);

                    setView(result.getData());
                }
            });
        }
    }

    private void setView(MineMessageModel model){
        String name =model.getName();
        String content= model.getContent();
        titleView.setText(name);
//        setContentView(R.layout.activity_account_login);
//        String content = "account.getContent()";
        if(content!=null&&content.contains("&lt;")){
            content = Html.fromHtml(content).toString();
        }
        webView.loadDataWithBaseURL(null,content, "text/html", "utf-8", null);
    }

    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_docment;
    }




}
